DataHandler.DataEncryptions

Provides methods to serialize, encrypt, decrypt, and deserialize objects using JSON + AES.

Shared Settings

  • _jsonOpts (JsonSerializerOptions):
    • IncludeFields = true
      Enables JSON serialization of public fields as well as properties.

Methods

PackData(object data, SecureData Key)

Serializes an object of type T to JSON, wraps it with its assembly-qualified type name, Base64-encodes, then encrypts via AES-GCM.

  • Parameters:

    • data: The object to serialize (must be castable to T).
    • Key: SecureData used to derive the AES encryption key.
  • Process:

    1. Convert data to a byte array via BinaryConverter.NCObjectToByteArrayAsync.
    2. Create a tuple (assemblyQualifiedTypeName, payloadBytes).
    3. Serialize the tuple to UTF-8 JSON bytes (JsonSerializer.SerializeToUtf8Bytes).
    4. Base64-encode the JSON bytes.
    5. Encrypt the Base64 string using SimpleAESEncryption.Encrypt.
    6. Return the encrypted payload as AESEncryptedText.ToString().
  • Returns: Task — The encrypted Base64 JSON wrapper as a single string.


UnpackData(string data, SecureData Key)

Decrypts and deserializes a string previously produced by PackData back into the original object.

  • Parameters:

    • data: The encrypted payload string (IV|EncryptedText format).
    • Key: SecureData used to derive the AES decryption key.
  • Process:

    1. Parse data into SimpleAESEncryption.AESEncryptedText.
    2. Decrypt to recover the Base64-encoded JSON wrapper.
    3. Base64-decode to JSON bytes.
    4. Deserialize JSON into (string assemblyQualifiedTypeName, byte[] payloadBytes).
    5. Resolve the Type from assemblyQualifiedTypeName.
    6. Use reflection to call BinaryConverter.NCByteArrayToObjectAsync on payloadBytes.
    7. Await and return the .Result of that task (the original object).
  • Returns: Task — The deserialized object instance.

  • Exceptions:

    • Throws if decryption fails, JSON is invalid, the type cannot be resolved, or deserialization errors occur.